using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
 public  class tbDoanhNghiepDAL
    {
     private SqlConnection cn = null;
        public tbDoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbDoanhNghiepDAL(tbDoanhNghiepDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
     public void Insert(tbDoanhNghiep doanhnghiep)
     {
         SqlCommand cm = new SqlCommand("tbDoanhNghiep_Insert", cn);
         cm.CommandType = CommandType.StoredProcedure;
         cm.Parameters.AddWithValue("@TenDN", doanhnghiep.TenDN);
         cm.Parameters.AddWithValue("@TaiKhoan", doanhnghiep.TaiKhoan);
         cm.Parameters.AddWithValue("@MatKhau", doanhnghiep.MatKhau);
         cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
         cm.Parameters.AddWithValue("@Website", doanhnghiep.Website);
         cm.Parameters.AddWithValue("@SoDT", doanhnghiep.SoDT);
         cm.Parameters.AddWithValue("@Email", doanhnghiep.Email);
         cm.ExecuteNonQuery();
     }
     public void Update(tbDoanhNghiep doanhnghiep)
     {
         SqlCommand cm = new SqlCommand("tbDoanhNghiep_Update", cn);
         cm.CommandType = CommandType.StoredProcedure;
         cm.Parameters.AddWithValue("@MaDN", doanhnghiep.MaDN);
         cm.Parameters.AddWithValue("@TenDN", doanhnghiep.TenDN);
         cm.Parameters.AddWithValue("@TaiKhoan", doanhnghiep.TaiKhoan);
         cm.Parameters.AddWithValue("@MatKhau", doanhnghiep.MatKhau);
         cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
         cm.Parameters.AddWithValue("@Website", doanhnghiep.Website);
         cm.Parameters.AddWithValue("@SoDT", doanhnghiep.SoDT);
         cm.Parameters.AddWithValue("@Email", doanhnghiep.Email);
         cm.ExecuteNonQuery();
     }
     public void Delete(int ID)
     {
         SqlCommand cm = new SqlCommand("tbDoanhNghiep_Detele", cn);
         cm.CommandType = CommandType.StoredProcedure;
         cm.Parameters.AddWithValue("@MaDN", ID);
         cm.ExecuteNonQuery();

     }
     public tbDoanhNghiepCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
     {
         SqlCommand cm = new SqlCommand("tbDoanhNghiep_GET", cn);
         cm.CommandType = CommandType.StoredProcedure;
         cm.Parameters.AddWithValue("@Where", where);
         cm.Parameters.AddWithValue("@PageIndex", pageIndex);
         cm.Parameters.AddWithValue("@PageSize", pageSize);
         cm.Parameters.AddWithValue("@OrderBy", orderBy);
         cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
         cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
         SqlDataReader dr = cm.ExecuteReader();
         tbDoanhNghiepCollection doanhNghiepCollection = new tbDoanhNghiepCollection();
         while (dr.Read())
         {
             tbDoanhNghiep Doanhnghiep = new tbDoanhNghiep();
             Doanhnghiep .MaDN  = int.Parse(dr.GetValue(0).ToString());             
             Doanhnghiep .TenDN  = dr.GetValue(1).ToString();
             Doanhnghiep.TaiKhoan = dr.GetValue(2).ToString();
             Doanhnghiep.MatKhau  = dr.GetValue(3).ToString();
             Doanhnghiep.DiaChi  = dr.GetValue(4).ToString();
             Doanhnghiep.Website  = dr.GetValue(5).ToString();
             Doanhnghiep.SoDT  = dr.GetValue(6).ToString();
             Doanhnghiep.Email  = dr.GetValue(7).ToString();
             doanhNghiepCollection.Add(Doanhnghiep);
         }
         dr.Close();
         totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
         return doanhNghiepCollection;


     }

    }
}
